package com.baoleme.baoleme_goods_service.controller;

import com.baoleme.baoleme_goods_service.service.CategoryService;
import com.baoleme.common.Result;
import com.baoleme_goods_model.dto.CategoryDTO;
import com.baoleme_goods_model.entity.Category;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * 修改分类
     * @param categoryDTO
     * @return 是否修改成功
     */
    @Operation(summary = "修改分类")
    @PutMapping
    public Result<Boolean> update(@RequestBody CategoryDTO categoryDTO){
        return Result.success(categoryService.updateCategory(categoryDTO));
    }

    /**
     * 分类分页查询
     * @param name
     * @param currentStr
     * @param pageSizeStr
     * @param type
     * @return
     */
    @Operation(summary = "分类分页查询")
    @GetMapping("page")
    public Result<IPage<Category>> page(@RequestParam(value = "name",required = false) String name, String currentStr, String pageSizeStr, @RequestParam(value = "type", required = false) String type){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if (name != null && !name.isEmpty()){
            wrapper.like(Category::getName, name);
        }
        if (type != null && !type.isEmpty()){
            wrapper.eq(Category::getType, type);
        }
        wrapper.orderByDesc(Category::getCreateTime);
        int current = Integer.parseInt(currentStr);
        int pageSize = Integer.parseInt(pageSizeStr);

        IPage<Category> pageInfo = new Page<>(current,pageSize);
        return Result.success(categoryService.page(pageInfo, wrapper));
    }

    /**
     * 启用、禁用分类
     * @param id 分类id
     * @param status 1为启用，0为禁用
     * @return 是否启用、禁用成功
     */
    @Operation(summary = "启用、禁用分类")
    @PostMapping("status/{status}")
    public Result<Boolean> status(String id, @PathVariable String status){
        return Result.success(categoryService.updateStatus(id, status));
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return 是否新增成功
     */
    @Operation(summary = "新增分类")
    @PostMapping
    public Result<Boolean> add(@RequestBody CategoryDTO categoryDTO){
        return Result.success(categoryService.saveCategory(categoryDTO));
    }

    /**
     * 根据id删除分类
     * @param id
     * @return 是否删除成功
     */
    @Operation(summary = "根据id删除分类")
    @DeleteMapping
    public Result<Boolean> deleteById(String id){
        return Result.success(categoryService.removeById(id));
    }

    /**
     * 根据类型查询分类
     * @param type 分类类型，不必须
     * @return 分类List
     */
    @Operation(summary = "根据类型查询分类")
    @GetMapping("list")
    public Result<List<Category>> list(@RequestParam(value = "type", required = false) String type){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if (type != null && !type.isEmpty()){
            wrapper.eq(Category::getType,type);
        }
        List<Category> list = categoryService.list(wrapper);
        return Result.success(list);
    }

}
